package 枚举与尺聚法;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class code39 {

    public static void main(String[] args) {
        List<List<Integer>> lists = combinationSum(new int[]{2, 3, 6, 7}, 7);
        for (List<Integer> list : lists) {
            System.out.println(list);
        }
    }

    public static List<List<Integer>> combinationSum(int[] candidates, int target) {
        List<List<Integer>> result = new ArrayList<>();
        dfs(0,candidates, target, result, new LinkedList<Integer>());
        return result;

    }

    static void dfs(int start, int[] candidates, int target, List<List<Integer>> result, LinkedList<Integer> linkedList) {
        if (target < 0) {
            return;
        }
        if (target == 0) {
            result.add(new ArrayList<>(linkedList));
            return;
        }

        for (int i = start; i < candidates.length; i++) {
            linkedList.push(candidates[i]);
            dfs(i,candidates, target - candidates[i], result, linkedList);
            linkedList.pop();
        }

    }
}
